<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>固定头部</title>
    <style>
        body{
            margin: 0;
        }
        .content{
            height: 2000px;
            width: 800px;
            margin: 0 auto;
            padding-top: 300px;
            background-color: antiquewhite;
        }
        .spikes{
            width: 300px;
            height: 300px;
            background-color: rgb(176, 161, 134);
        }
        .topbar{
            transition: all 0.7s;
            height: 100px;
            width: 100%;
            font-size: 25px;
            text-align: center;
            line-height: 100px;
            background-color: skyblue;
            position: fixed;
            top: -100px;
            left: 0;
        }
    </style>
</head>
<body>
    <div class="topbar">头部导航</div>
    <div class="content">
        <div class="spikes">京东秒杀</div>
    </div>
    <script>
        const sk=document.querySelector('.spikes')
        const topbar=document.querySelector('.topbar')
        // 页面滚动事件
        window.addEventListener('scroll',function(){
            // 页面滚动到秒杀模块时，显示头部导航
            const n=document.documentElement.scrollTop

            topbar.style.top=n>=sk.offsetTop?0:'-100px'
            // if(n>=sk.offsetTop){
            //     topbar.style.top=0
            // }else{
            //     topbar.style.top='-100px'
            // }
        })
    </script>
</body>
</html>